package com.test.config.page;


import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import javax.validation.Valid;


/**
 * @author lijinze
 * 分页参数传入
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class PageReq<T> implements BasePage {

    @ApiModelProperty(value = "页码",example ="1" )
    private Integer pageNum = DEFAULT_PAGE_NUM;

    @ApiModelProperty(value = "页数",example = "10")
    private Integer pageSize = DEFAULT_PAGE_SIZE;

    @ApiModelProperty(value = "排序")
    private String orderBy;

    @Valid
    @ApiModelProperty(value = "参数")
    private T param;

    public static <T> PageParamBuilder<T> builder(T param) {
        return new PageParamBuilder<>(param);
    }

    //todo order by 有sql注入问题暂时禁用
    public PageReq<T> setOrderBy(String orderBy) {

//        this.orderBy = orderBy;
        return this;
    }

    public static class PageParamBuilder<T> {
        private final T param;
        private Integer pageNum;
        private Integer pageSize;
        private String orderBy;

        PageParamBuilder(T param) {
            this.param = param;
        }

        public PageParamBuilder<T> pageNum(Integer pageNum) {
            this.pageNum = pageNum;
            return this;
        }

        public PageParamBuilder<T> pageSize(Integer pageSize) {
            this.pageSize = pageSize;
            return this;
        }


        public PageReq<T> build() {
            return new PageReq<T>(pageNum, pageSize, orderBy, param);
        }

    }
}
